import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:series_play/generated/assets.dart';
import 'package:series_play/utils/log.dart';
import 'package:series_play/widget/app_color.dart';

class AppImage extends StatelessWidget {
  const AppImage(
    this.src, {
    this.size,
    this.width,
    this.height,
    this.boxFit = BoxFit.contain,
    this.color,
    this.isCircle = false,
    this.isRound = false,
    this.borderColor,
    this.borderWidth,
    this.onTap,
    this.borderRadius,
    this.isShowShadow,
    super.key,
  });

  final String src;
  final double? size;
  final double? width;
  final double? height;
  final BoxFit boxFit;
  final Color? color;
  final bool? isCircle;
  final bool? isRound;

  final BorderRadiusGeometry? borderRadius;
  final Color? borderColor;

  final double? borderWidth;
  final VoidCallback? onTap;

  final bool? isShowShadow;

  @override
  Widget build(BuildContext context) {
    final mWidth = size ?? width ?? 18;
    final mHeight = size ?? height ?? 18;
    Widget child = Container();

    final isRemoteImage =
        src.startsWith('https://') || src.startsWith('http://');

    if (isRemoteImage) {
      // remote rul 是否需要构建header
      if (src.endsWith('.svg')) {
        child = SvgPicture.network(
          src,
          width: mWidth.w,
          height: mHeight.w,
          fit: boxFit,
        );
      } else {
        // 设置下解码大小限制
        final dpr = MediaQuery.of(context).devicePixelRatio;
        // 最大为设计图大小
        final memCacheHeight =
            (height == double.infinity ? 815.w : mHeight.w * dpr).toInt();

        child = CachedNetworkImage(
          imageUrl: src,
          width: mWidth.w,
          height: mHeight.w,
          fit: boxFit,
          fadeInCurve: Curves.linear,
          fadeOutCurve: Curves.linear,
          fadeInDuration: 150.ms,
          fadeOutDuration: 150.ms,
          memCacheHeight: memCacheHeight,
          progressIndicatorBuilder: (context , url , progress){
            if(progress.totalSize == null && progress.progress == null){
              return Image.asset(
                Assets.imagesImgLoadFail,
                width: mWidth.w,
                height: mHeight.w,
                fit: BoxFit.contain,
              );
            }
            return CircularProgressIndicator(
              color: AppColor.primary,
              strokeWidth: 2.w,
              strokeCap: StrokeCap.round,
            );
          },
          errorWidget: (context, url, error) {
            return Image.asset(
              Assets.imagesImgLoadFail,
              width: mWidth.w,
              height: mHeight.w,
              fit: BoxFit.contain,
            );
          },
        );
      }
    } else {
      if (src.endsWith('.svg')) {
        child = SvgPicture.asset(
          src,
          width: mWidth.w,
          height: mHeight.w,
          fit: boxFit,
        );
      } else {
        child = Image.asset(
          src,
          width: mWidth.w,
          height: mHeight.w,
          color: color,
          fit: boxFit,
        );
      }
    }

    if (isCircle ?? false) {
      child = Container(
        width: (mWidth + borderWidth!).w,
        height: (mHeight + borderWidth!).w,
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(
            color: borderColor ?? Colors.white,
            width: borderWidth ?? 0,
          ),
        ),
        child: ClipOval(child: child),
      );
    }

    if (isRound ?? false) {
      child = ClipRRect(
        borderRadius: borderRadius ?? BorderRadius.zero,
        child: child,
      );
    }

    if (isShowShadow ?? false) {
      child = Container(
        decoration: BoxDecoration(
          borderRadius: borderRadius,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.2), // 阴影颜色
              blurRadius: 2, // 模糊半径
              offset: Offset(0, 1), // 阴影偏移 (x, y)
              spreadRadius: 2.0, // 扩展半径
            ),
          ],
        ),
        child: child,
      );
    }

    if (onTap != null) {
      return GestureDetector(onTap: onTap, child: child);
    }
    return child;
  }

  factory AppImage.assetsWith18(
    String src, {
    BoxFit? boxFit,
    Color? color,
    VoidCallback? onTap,
  }) {
    return AppImage(
      src,
      size: 18,
      boxFit: boxFit ?? BoxFit.contain,
      color: color,
      onTap: onTap,
    );
  }

  factory AppImage.assetsWith15(
    String src, {
    BoxFit? boxFit,
    Color? color,
    VoidCallback? onTap,
  }) {
    return AppImage(
      src,
      size: 15,
      boxFit: boxFit ?? BoxFit.contain,
      color: color,
      onTap: onTap,
    );
  }

  factory AppImage.circle(
    String src, {
    double? size,
    double? width,
    double? height,
    BoxFit? boxFit,
    Color? color,
    Color? borderColor,
    double? borderWidth,
    VoidCallback? onTap,
  }) {
    return AppImage(
      src,
      size: size,
      width: width,
      height: height,
      boxFit: boxFit ?? BoxFit.contain,
      color: color,
      isCircle: true,
      borderWidth: borderWidth ?? 2,
      borderColor: borderColor ?? AppColor.white_40,
      onTap: onTap,
    );
  }

  factory AppImage.round(
    String src, {
    double? size,
    double? width,
    double? height,
    BoxFit? boxFit,
    Color? color,
    BorderRadiusGeometry? borderRadius,
    Color? borderColor,
    double? borderWidth,
    VoidCallback? onTap,
  }) {
    return AppImage(
      src,
      size: size,
      width: width,
      height: height,
      boxFit: boxFit ?? BoxFit.cover,
      color: color,
      isRound: true,
      borderRadius: borderRadius ?? BorderRadius.circular(8).r,
      onTap: onTap,
    );
  }
}
